package school.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import school.domain.Info;
import school.domain.User;
import school.mapper.InfoMapper;
import school.mapper.UserMapper;
import school.service.UserService;
import school.support.BaseController;
import school.support.util.VerifyCodeUtil;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private InfoMapper infoMapper;

    @GetMapping({"index", "/"})
    public String index() {
        return "index";
    }


    /**
     * 到登录页面
     *
     * @return
     */
    @GetMapping("login")
    public String login() {
        return "login";
    }

    /**
     * 登录
     *
     * @return
     */
    @PostMapping("login")
    public String login(String username, String password, String code) {
        if (!code.equalsIgnoreCase((String) session.getAttribute("verifyCode"))) {
            request.setAttribute("msg", "验证码错误");
            return "login";
        }
        User user = userService.login(username, password);
        if (null == user) {
            request.setAttribute("msg", "用户名或者密码错误");
            return "login";
        }
        session.setAttribute("sessionUser", user);
        if ("admin".equals(user.getType())) return "redirect:/index";
        return "redirect:index";
    }


    /**
     * 到注册页面
     *
     * @return
     */
    @GetMapping("register")
    public String register() {
        return "register";
    }

    /**
     * 注册
     *
     * @param user
     * @return
     */
    @PostMapping("register")
    public String register(User user, String password2, String code) {
        if (!code.equalsIgnoreCase((String) session.getAttribute("verifyCode"))) {
            request.setAttribute("msg", "验证码错误");
            return "register";
        }
        if (!user.getPassword().equals(password2)) {
            request.setAttribute("msg", "两次密码不一样");
            return "register";
        }
        //判断用户是否存在
        User temp = new User();
        temp.setName(user.getName());
        int size = userMapper.select(temp).size();
        if (size > 0) {
            request.setAttribute("msg", "用户名已经存在");
            return "register";
        }

        user.setAddtime(new Date());
        user.setType("user");
        userMapper.insertSelective(user);
        return "redirect:index";
    }

    /**
     * 退出登录
     *
     * @return
     */
    @RequestMapping("logout")
    public String logout() {
        session.removeAttribute("sessionUser");
        return "redirect:index";
    }


    /**
     * 验证码
     *
     * @param response
     * @throws IOException
     * @Date 2016年8月23日下午2:31:11
     */
    @RequestMapping("verify")
    public void verify(HttpServletResponse response) throws IOException {
        String code = VerifyCodeUtil.outputVerifyImage(144, 42, response.getOutputStream(), 5);
        request.getSession().setAttribute("verifyCode", code);
    }

    /**
     * 修改密码
     *
     * @param pwd
     * @param pwd2
     * @return
     */
    @RequestMapping("modify")
    public String modify(String pwd, String pwd2, HttpSession session, RedirectAttributes model) {
        User user = (User) session.getAttribute("sessionUser");
        if (!user.getPassword().equals(pwd)) {
            model.addAttribute("errorMsg", "原密码错误");
            return refresh();
        }
        user.setPassword(pwd2);
        userMapper.updateByPrimaryKeySelective(user);
        session.setAttribute("sessionUser", user);
        return "redirect:/index";
    }

    /**
     * 个人信息
     *
     * @param model
     * @return
     */
    @RequestMapping("info")
    public String info(Model model) {
        User user = userMapper.selectByPrimaryKey(sessionUser().getId());
        model.addAttribute(user);
        return "info";
    }

    /**
     * 更新用户信息
     * 登录用户的信息
     *
     * @param user
     * @return
     */
    @RequestMapping("update")
    public String update(User user) {
        user.setId(sessionUser().getId());
        userMapper.updateByPrimaryKeySelective(user);
        return refresh();
    }

    /**
     * BMI 扇形图
     *
     * @return
     */
    @RequestMapping("tu")
    public String tu(Model model) {
        //获取用户最新的bmi值
        List<Info> list = infoMapper.selectLast();
        //
        int gq = 0, zc = 0, gz = 0, fp = 0, fcfp = 0;
        for (Info info : list) {
            /**
             * 过轻：低于18.5
             正常：18.5-23.9
             过重：24-27
             肥胖：28-32
             非常肥胖, 高于32
             */
            if (info.getBmi() < 18.5) gq++;
            else if (info.getBmi() < 23.9) zc++;
            else if (info.getBmi() < 27) gz++;
            else if (info.getBmi() < 32) fp++;
            else fcfp++;
        }
        model.addAttribute("gq",gq);
        model.addAttribute("zc",zc);
        model.addAttribute("gz",gz);
        model.addAttribute("fp",fp);
        model.addAttribute("fcfp",fcfp);
        return "tu";
    }
}
